import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {UserIdentity} from '@/base/model/UserIdentity';
import {Page} from '@/base/Page';
import {Account} from '@/base/model/Account';
import {TableSort} from '@/base/TableSort';

export function getAccountList(params: {
  pageIndex: number,
  pageSize: number,
  userName?: string,
  phone?: string,
  email?: string,
  createBegin?: string,
  createEnd?: string,
  companyId?: number[],
  roleId?: string,
  type?: string
}): Promise<Result<Page<Account>>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getPage(Account, '/api/account?' + companyIdString, {params});
}

export function getAgents(params: {
  pageIndex: number,
  pageSize: number,
  userName?: string,
  phone?: string,
  email?: string,
  createBegin?: string,
  createEnd?: string,
  companyId?: number[],
  roleId?: string
}): Promise<Result<Page<Account>>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getPage(Account, '/api/account/fetchAgents?' + companyIdString, {params});
}

export function getAccount(id: number): Promise<Result<Account>> {
  return request.get(Account, `/api/account/${id}`);
}

// 获取当前用户凭证
export function getAccoun(): Promise<Result<UserIdentity>> {
  return request.get(UserIdentity, '/api/account/accoun');
}

// 获取品牌商绑定的用户数量
export function getCompanyNum(params: {
  companyId: number
}): Promise<Result<Number>> {
  return request.get(Number, '/api/account/company-num', {params});
}

export function putAccount(id: number, params: {
  email: string,
  password: string,
  phone: string,
  roles: string[],
  userName: string,
  type?: string
}): Promise<Result<Account>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Account, `/api/account/${id}`, formData);
}

export function putAccountUserPwd(params: {
  id: number,
  password: string
}): Promise<Result<Account>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Account, `/api/account/user-password`, formData);
}

export function putAccountPwd(params: {
  oldPwd: string,
  password: string
}): Promise<Result<Account>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Account, `/api/account/password`, formData);
}

export function postAccount(params: {
  email: string,
  password: string,
  roles: string[],
  phone: string,
  userName: string,
  type?: string
}): Promise<Result<Account>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Account, '/api/account', formData);
}

export function deleteAccount(id: number): Promise<Result<void>> {
  return request.delete(`/api/account/${id}`);
}
